package com.necsv;

import java.util.List;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Tutorial1Activity extends Activity	 {
	private static final String TAG = "Tutorial1Activity";
	private RadioGroup rg1;
	private EditText txt1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tutorial1);
		rg1 = (RadioGroup)findViewById(R.id.Tutorial1RG1);
		txt1 = (EditText)findViewById(R.id.tutorial1Txt1);
		rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.tutorial1Rd1:
					txt1.setTextColor(Color.RED);
					break;
				case R.id.tutorial1Rd2:
					txt1.setTextColor(Color.GREEN);
					break;
				case R.id.tutorial1Rd3:
					txt1.setTextColor(Color.BLUE);
					break;

				default:
					break;
				}
			}
		});
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		GoogleAnaTool.trackPage("/Tutorial1");
		GoogleAnaTool.trackReferrer("trackReferrerTutorial1");
		GoogleAnaTool.trackReferrer("gclid=gclidValue");
		GoogleAnaTool.trackReferrer("utm_campaign=campaign&utm_source=source&utm_medium=medium&utm_term=term&utm_content=content");
		super.onResume();
	}
	
	public void btn_onClick(View v) {
		DefaultHttpClient client = new DefaultHttpClient();
		final List<Cookie> cookies = client.getCookieStore().getCookies();
		int count = cookies.size();
		Log.v(TAG,"Cookies count:" + count);
		if (cookies.isEmpty()) {
			Log.v(TAG, "Empty");
			String myUrl = "http://mydomain.com/";
			CookieSyncManager.createInstance(this);
			CookieManager cookieManager = CookieManager.getInstance();
			// Cookie sessionCookie = this.getCookie();
			cookieManager.setCookie(myUrl, "thanhson");
			CookieSyncManager.getInstance().sync(); 
			Log.v(TAG, "setcookie OK:" + cookieManager.getCookie(myUrl));
        } else {
            for (int i = 0; i < cookies.size(); i++) {
            	Log.v(TAG,"- " + cookies.get(i).toString());
            }
        }
	}

}
